//
//  SessionToken.swift
//
//
//  Created by Jiang Chencheng on 2024/9/2.
//

import Vapor
import JWT

struct SessionToken: Content, Authenticatable, JWTPayload {

    // Constants
    var expirationTime: TimeInterval = 60 * 15

    // Token Data
    var expiration: ExpirationClaim
    var userId: UUID
    
    init(userId: UUID) {
        self.userId = userId
        self.expiration = ExpirationClaim(value: Date().addingTimeInterval(expirationTime))
    }

    init(with user: User) throws {
        self.userId = try user.requireID()
        self.expiration = ExpirationClaim(value: Date().addingTimeInterval(expirationTime))
    }
   
    func verify(using signer: JWTKit.JWTSigner) throws {
        try expiration.verifyNotExpired()
    }
}

struct ClientTokenResponse: Content {
    var token: String
}
